package com.atguigu.champter5.Flink05.transformation.aggregation;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class $5_Connect_UNion {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        DataStreamSource<Integer> dss1 = env.fromElements(1, 2, 3, 4);
        DataStreamSource<Integer> dss1_5 = env.fromElements(44, 33,22 , 11);
        DataStreamSource<String> dss2 = env.fromElements("a", "b", "c", "d");
        ConnectedStreams<Integer, String> cs = dss1.connect(dss2);
        /*
        * <IN1> – Type of the first input.
        *  <IN2> – Type of the second input.
        * <OUT> – Output type.
        * */
        cs.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return value + "数字";
            }

            @Override
            public String map2(String value) throws Exception {
                return value+"字符";
            }
        });

        /**
         * 1.union之前两个或多个流的类型必须是一样，connect可以不一样
         * 2.connect只能操作两个流，union可以操作多个。
         */

        dss1.union(dss1_5).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
